﻿// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "miHoYo/Character/Illusory" {
	Properties {
		_MainColor ("Main Color", Color) = (1,1,1,0.5)
		_ColorScaler ("Color Scaler", float) = 1
		_BloomFactor ("Bloom Factor", float) = 1
		_Opaqueness ("Opaqueness", Range(0, 1)) = 0.5

		//Rim
		_RimShininess ("Rim Shininess", float) = 10
		_RimThreshold ("Rim Threshold", Range(0, 1)) = 0.5
		_RimIntensity ("Rim Intensity", Range(0, 10)) = 0.5
	}
	SubShader {
		Tags {
			"Queue"="Transparent"
		 	"RenderType"="Transparent" 
		}

		//Back Face Pass
		Pass
		{
			Cull Front
			Blend SrcAlpha OneMinusSrcAlpha
			Lighting Off

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
			#include "Character_Common.cginc"

			half _ColorScaler;

			struct appdata
			{
				float4 vertex : POSITION;
			};

			struct v2f
			{
				float4 position : SV_POSITION;
			};

			v2f vert(appdata inData)
			{
				v2f outData;

				outData.position = UnityObjectToClipPos(inData.vertex);

				return outData;
			}

			half4 frag(v2f inData) : SV_Target
			{
				half4 color;

				color.rgb = _MainColor.rgb * _ColorScaler;
				color.a = _Opaqueness;

				return color;
			}

			ENDCG
		}


		//Front Face Pass
		Pass
		{
			Cull Back
			Blend SrcAlpha OneMinusSrcAlpha
			Lighting Off

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"
			#include "Character_Common.cginc"

			half _ColorScaler;

			half _RimShininess;
			fixed _RimThreshold;
			fixed _RimIntensity;

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal : TANGENT;
			};

			struct v2f
			{
				float4 position : SV_POSITION;
				half3 normal : TEXCOORD0;
				half3 posCam : TEXCOORD1;
			};

			v2f vert(appdata inData)
			{
				v2f outData;

				outData.posCam = mul(UNITY_MATRIX_MV, inData.vertex);
				outData.normal = mul((float3x3)UNITY_MATRIX_MV, inData.normal);
				outData.position = UnityObjectToClipPos(inData.vertex);

				return outData;
			}

			half4 frag(v2f inData) : SV_Target
			{
				half4 color;

				half rim = rim_factor(normalize(inData.normal), -normalize(inData.posCam), _RimShininess);
				if (rim > _RimThreshold)
				{
					rim = _RimIntensity;  
				}
				else
				{
					rim = 0;
				}

				color.rgb = (1.0f + rim) * _MainColor.rgb * _ColorScaler;
				color.a = _Opaqueness;

				return color;
			}

			ENDCG
		}

		//Constant Replace Bloom Pass Without Cutoff
		Pass
		{
			Name "CONSTANT_REPLACE_NOCUTOFF"
			ColorMask A

			CGPROGRAM
			#include "../Common/Common_Bloom.cginc"
			#pragma vertex vert
			#pragma fragment frag
			ENDCG
		} // End of Bloom Pass
	} 
}
